home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / miscgl.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  394 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    miscgl - 
  19.  *        Miscellaneous stuff that relies on the gl.
  20.  *
  21.  *                Paul Haeberli - 1988
  22.  */
  23. #include "gl.h"
  24. #include "device.h"
  25. #include "stdio.h"
  26. #include "values.h"
  27. #include "math.h"
  28.  
  29. static makecirc();
  30.  
  31. drawfunc(f)
  32. int (*f)();
  33. {
  34.     short val;
  35.  
  36.     f();
  37.     qdevice(ESCKEY);
  38.     while (1) {
  39.     switch(qread(&val)) {
  40.         case ESCKEY:
  41.         exit(1);
  42.         break;
  43.         case REDRAW:
  44.         qreset();
  45.         f();
  46.         break;
  47.     }
  48.     }
  49. }
  50.  
  51. qmouse()
  52. {
  53.     qdevice(MOUSEX);
  54.     qdevice(MOUSEY);
  55.     qdevice(LEFTMOUSE);
  56.     qdevice(MIDDLEMOUSE);
  57.     qdevice(RIGHTMOUSE);
  58. }
  59.  
  60. getmousex()
  61. {
  62.     long xorg, yorg;
  63.  
  64.     getorigin(&xorg,&yorg);
  65.     return getvaluator(MOUSEX)-xorg;
  66. }
  67.  
  68. getmousey()
  69. {
  70.     long xorg, yorg;
  71.  
  72.     getorigin(&xorg,&yorg);
  73.     return getvaluator(MOUSEY)-yorg;
  74. }
  75.  
  76. float fgetmousex()
  77. {
  78.     long xorg, yorg;
  79.     long xsize, ysize;
  80.  
  81.     getsize(&xsize,&ysize);
  82.     getorigin(&xorg,&yorg);
  83.     return ((float)getvaluator(MOUSEX)-xorg)/(float)xsize;
  84. }
  85.  
  86. float fgetmousey()
  87. {
  88.     long xorg, yorg;
  89.     long xsize, ysize;
  90.  
  91.     getsize(&xsize,&ysize);
  92.     getorigin(&xorg,&yorg);
  93.     return ((float)getvaluator(MOUSEY)-yorg)/(float)ysize;
  94. }
  95.  
  96. printsize()
  97. {
  98.     long xsize, ysize;
  99.  
  100.     getsize(&xsize,&ysize);
  101.     printf("size is %d %d\n",xsize,ysize);
  102. }
  103.  
  104. printorigin()
  105. {
  106.     long xorg, yorg;
  107.  
  108.     getorigin(&xorg,&yorg);
  109.     printf("size is %d %d\n",xorg,yorg);
  110. }
  111.  
  112. /*
  113.  *    redraw -
  114.  *        Test for a redraw token in the queue, and if present, 
  115.  *     swallow it and call the optionally supplied user function as well 
  116.  *    as the code to reshape the viewport based on the new size.
  117.  *
  118.  *                Kipp Hickman - 1985
  119.  */
  120. redraw(f)
  121. int (*f)();
  122. {
  123.     int retval;
  124.     short v;
  125.  
  126.     retval = 0;
  127.     while (qtest()) {
  128.     switch (qread(&v)) {
  129.         case REDRAW:
  130.         reshapeviewport();
  131.         if (f)
  132.             (*f)();
  133.         retval = 1;
  134.         break;
  135.     }
  136.     }
  137.     return retval;
  138. }
  139.  
  140. /*
  141.  *    subview -
  142.  *        Some support for viewports inside the graph port.
  143.  *
  144.  *                Henry Moreton - 1984
  145.  *
  146.  */
  147. subviewport(left, right, bottom, top)
  148. long left, right, bottom, top;
  149. {
  150.     int xmaxscreen, ymaxscreen;
  151.  
  152.     xmaxscreen = getgdesc(GD_XPMAX)-1;
  153.     ymaxscreen = getgdesc(GD_YPMAX)-1;
  154.     subport((float)left/xmaxscreen, (float)right/xmaxscreen, 
  155.          (float)bottom/ymaxscreen, (float)top/ymaxscreen);
  156. }
  157.  
  158. subport(left, right, bottom, top)
  159. float left, right, bottom, top;
  160. {
  161.     short curr_left, curr_right, curr_bottom, curr_top;
  162.     short new_left, new_right, new_bottom, new_top;
  163.     int width, height;
  164.  
  165.     getviewport(&curr_left,&curr_right,&curr_bottom,&curr_top);
  166.  
  167.     /* calculate the new viewport size and position based on the
  168.     current viewport and the requested subviewport */
  169.  
  170.     height = (curr_top - curr_bottom +1);
  171.     width = (curr_right - curr_left +1);
  172.     new_right = curr_left + (right * width) + 0.5;
  173.     new_left = curr_left + (left * width) + 0.5;
  174.     new_top = curr_bottom + (top * height) + 0.5;
  175.     new_bottom = curr_bottom + (bottom * height) + 0.5;
  176.     viewport(new_left, new_right, new_bottom, new_top);
  177. }
  178.  
  179. fillrect(x1,y1,x2,y2)
  180. float x1,y1,x2,y2;
  181. {
  182.     float v[2];
  183.  
  184.     bgnpolygon();
  185.     v[0] = x1;
  186.     v[1] = y1;
  187.     v2f(v);
  188.     v[0] = x2;
  189.     v2f(v);
  190.     v[1] = y2;
  191.     v2f(v);
  192.     v[0] = x1;
  193.     v2f(v);
  194.     endpolygon();
  195. }
  196.  
  197. drawrect(x1,y1,x2,y2)
  198. float x1,y1,x2,y2;
  199. {
  200.     float v[2];
  201.  
  202.     bgnclosedline();
  203.     v[0] = x1;
  204.     v[1] = y1;
  205.     v2f(v);
  206.     v[0] = x2;
  207.     v2f(v);
  208.     v[1] = y2;
  209.     v2f(v);
  210.     v[0] = x1;
  211.     v2f(v);
  212.     endclosedline();
  213. }
  214.  
  215. fillrecti(x1,y1,x2,y2)
  216. int x1,y1,x2,y2;
  217. {
  218.     long v[2];
  219.  
  220.     bgnpolygon();
  221.     v[0] = x1;
  222.     v[1] = y1;
  223.     v2i(v);
  224.     v[0] = x2;
  225.     v2i(v);
  226.     v[1] = y2;
  227.     v2i(v);
  228.     v[0] = x1;
  229.     v2i(v);
  230.     endpolygon();
  231. }
  232.  
  233. drawrecti(x1,y1,x2,y2)
  234. int x1,y1,x2,y2;
  235. {
  236.     long v[2];
  237.  
  238.     bgnclosedline();
  239.     v[0] = x1;
  240.     v[1] = y1;
  241.     v2i(v);
  242.     v[0] = x2;
  243.     v2i(v);
  244.     v[1] = y2;
  245.     v2i(v);
  246.     v[0] = x1;
  247.     v2i(v);
  248.     endclosedline();
  249. }
  250.  
  251. fillcirc(x,y,rad)
  252. float x, y, rad;
  253. {
  254.     pushmatrix();
  255.     translate(x,y,0.0);
  256.     scale(rad,rad,0.0);
  257.     bgnpolygon();
  258.     makecirc();
  259.     endpolygon();
  260.     popmatrix();
  261. }
  262.  
  263. drawcirc(x,y,rad)
  264. float x, y, rad;
  265. {
  266.     pushmatrix();
  267.     translate(x,y,0.0);
  268.     scale(rad,rad,0.0);
  269.     bgnclosedline();
  270.     makecirc();
  271.     endclosedline();
  272.     popmatrix();
  273. }
  274.  
  275. static nsides = 0;
  276. static float *circcoords;
  277.  
  278. circsides(n)
  279. int n;
  280. {
  281.     int i;
  282.     float a;
  283.  
  284.     if(n != nsides) {
  285.     nsides = n;
  286.     if(circcoords) 
  287.         free(circcoords);
  288.     circcoords = (float *)mymalloc(2*nsides*sizeof(float));
  289.     for(i=0; i<nsides; i++) {
  290.         a = ((i)*2*M_PI)/nsides;
  291.         circcoords[2*i+0] = sin(a);
  292.         circcoords[2*i+1] = cos(a);
  293.     }
  294.     }
  295. }
  296.  
  297. static makecirc()
  298. {
  299.     int i, n;
  300.     float *fptr;
  301.  
  302.     if(!circcoords)
  303.     circsides(40);
  304.     fptr = circcoords;
  305.     n = nsides;
  306.     while(n>8) {
  307.     v2f(fptr+0);
  308.     v2f(fptr+2);
  309.     v2f(fptr+4);
  310.     v2f(fptr+6);
  311.     v2f(fptr+8);
  312.     v2f(fptr+10);
  313.     v2f(fptr+12);
  314.     v2f(fptr+14);
  315.     fptr += 16;
  316.     n-=8;
  317.     }
  318.     while(n--) {
  319.     v2f(fptr);
  320.     fptr += 2;
  321.     }
  322. }
  323.  
  324. shiftdown()
  325. {
  326.     if(getbutton(LEFTSHIFTKEY) || getbutton(RIGHTSHIFTKEY))
  327.     return 1;
  328.     else
  329.     return 0;
  330. }
  331.  
  332. optiondown()
  333. {
  334.     if(getbutton(LEFTALTKEY) || getbutton(RIGHTALTKEY))
  335.     return 1;
  336.     else
  337.     return 0;
  338. }
  339.  
  340. spacedown()
  341. {
  342.     if(getbutton(SPACEKEY))
  343.     return 1;
  344.     else
  345.     return 0;
  346. }
  347.  
  348. autoprefsize(wxsize,wysize)
  349. int wxsize, wysize;
  350. {
  351.     int sxsize, sysize;
  352.     int llx, lly, urx, ury;
  353.  
  354.     sxsize = getgdesc(GD_XPMAX);
  355.     sysize = getgdesc(GD_YPMAX);
  356.     llx = (sxsize-wxsize)/2;
  357.     lly = (sysize-wysize)/2;
  358.     urx = llx+wxsize-1;
  359.     ury = lly+wysize-1;
  360.     prefposition(llx,urx,lly,ury);
  361. }
  362.  
  363. wideline(x1,y1,x2,y2,rad)
  364. float x1, y1, x2, y2, rad;
  365. {
  366.     float dx, dy, rx, ry, mag;
  367.     float v[2];
  368.  
  369.     fillcirc(x1,y1,rad);
  370.     fillcirc(x2,y2,rad);
  371.     dx = x2-x1;
  372.     dy = y2-y1;
  373.     mag = sqrt(dx*dx+dy*dy);
  374.     if(mag>0.000001) {
  375.     rx = rad*dy/mag;
  376.     ry = -rad*dx/mag;
  377.     bgnpolygon();
  378.     v[0] = x1+rx;
  379.     v[1] = y1+ry;
  380.     v2f(v);
  381.     v[0] = x2+rx;
  382.     v[1] = y2+ry;
  383.     v2f(v);
  384.     v[0] = x2-rx;
  385.     v[1] = y2-ry;
  386.     v2f(v);
  387.     v[0] = x1-rx;
  388.     v[1] = y1-ry;
  389.     v2f(v);
  390.     endpolygon();
  391.     }
  392. }
  393.  
  394.